package org.wheel.plugins.starter.jdbc.pool.config;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.ResourcePatternUtils;

@EnableConfigurationProperties({DaoConfigProperties.class})
//@Import({ResourceLoader.class})
public class XmlMapperLoaderAutoConfiguration {

    // 直接使用 LogFactory 创建日志对象，Hutool 会自动检测项目中的日志框架（优先级：Slf4j > Log4j > Logback > JDK Logging）
    private static final Log log = LogFactory.get();

    // 供别的 Bean 拿
    private final DaoConfigProperties properties;

    // 供别的 Bean 拿
    @Bean
    public DaoConfigProperties daoConfigProperties() {
        return properties;
    }

    @Bean
    @ConditionalOnMissingBean
    public ResourceLoader resourceLoader() {
        return new DefaultResourceLoader(); // 使用默认实现
    }

//    public DaoAutoConfiguration(DaoConfigProperties properties ,
//                                ResourceLoader resourceLoader) throws IOException, XPathExpressionException,
//            ParserConfigurationException, SAXException {
//        System.out.println("====================   DaoAutoConfiguration  ========================");
//        this.properties = properties;
//        // 1. 解析 XML pattern = classpath*:mapper/*.xml
//        for (String pattern : properties.getXmlPath()){
//            Resource[] resources = ResourcePatternUtils.getResourcePatternResolver(resourceLoader)
//                    .getResources(pattern);
//            XmlMapperLoader.load(resources);
//        }
//    }


//    public DaoAutoConfiguration(DaoConfigProperties properties) throws IOException, XPathExpressionException,
//            ParserConfigurationException, SAXException {
//        System.out.println("====================   DaoAutoConfiguration  ========================");
//        this.properties = properties;
//        // 1. 解析 XML pattern = classpath*:mapper/*.xml
//
//        ResourceLoader resourceLoader = new DefaultResourceLoader();
//        ResourcePatternResolver resolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader);
//        for (String pattern : properties.getXmlPath()) {
//             XmlMapperLoader.load(resolver.getResources(pattern));
//        }
//    }

    //    public DaoAutoConfiguration(){
//        log.info("====================   DaoAutoConfiguration 1  ========================");
//        this.properties = new DaoConfigProperties();
//    }
    public XmlMapperLoaderAutoConfiguration(DaoConfigProperties properties,
                                ResourceLoader resourceLoader){
        log.trace("====================   DaoAutoConfiguration 2 ========================");
        this.properties = properties;
//        ResourceLoader resourceLoader = resourceLoader();
        // 1. 解析 XML pattern = classpath*:mapper/*.xml
        for (String pattern : properties.getXmlPath()){
            try{
                Resource[] resources = ResourcePatternUtils.getResourcePatternResolver(resourceLoader)
                        .getResources(pattern);

//                Resource[] resources = applicationContext.getResources(pattern);
                XmlMapperLoader.load(resources);
            } catch (Exception e) {
                log.error("无法加载 XML 配置文件：" + pattern, e);
            }
        }
    }
}
